<template>
	<el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
		<el-form :model="registerForm" :rules="registerRules" ref="ruleForm" label-width="100px" class="registerForm">
			<el-form-item label="注册账号" prop="userName">
				<el-input v-model="registerForm.userName"></el-input>
			</el-form-item>
			<el-form-item label="用户类型" prop="userType">
				<el-select v-model="registerForm.userType" placeholder="请选择活动区域">
					<el-option label="区县用户" value="area"></el-option>
					<el-option label="专家" value="zj"></el-option>
				</el-select>
			</el-form-item>
			<el-form-item label="省份">
				<el-col :span="8">
					<el-form-item prop="province">
						<el-select v-model="registerForm.provinceId" placeholder="请选择" @change="selectProvince">
							<el-option v-for="province in provinceList" :key="province.areaId"
								:label="province.areaName" :value="province.areaId"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="city">
						<el-select v-model="registerForm.cityId" placeholder="请选择" @change="selectCity">
							<el-option v-for="city in cityList" :key="city.areaId" :label="city.areaName"
								:value="city.areaId"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
				<el-col :span="8">
					<el-form-item prop="area">
						<el-select v-model="registerForm.areaId" placeholder="请选择">
							<el-option v-for="area in areaList" :key="area.areaId" :label="area.areaName"
								:value="area.areaId"></el-option>
						</el-select>
					</el-form-item>
				</el-col>
			</el-form-item>
			<el-form-item label="账号密码" prop="password">
				<el-input v-model="registerForm.password"></el-input>
			</el-form-item>
			<el-form-item label="确认密码" prop="passwordAgian">
				<el-input v-model="registerForm.passwordAgian"></el-input>
			</el-form-item>
			<el-form-item label="手机号" prop="mobile">
				<el-input v-model="registerForm.mobile"></el-input>
			</el-form-item>
			<el-form-item label="微信号" prop="wxid">
				<el-input v-model="registerForm.wxid"></el-input>
			</el-form-item>
		</el-form>
		<span slot="footer" class="dialog-footer">
			<el-button @click="visible = false">取消</el-button>
			<el-button type="primary" @click="dataFormSubmit()">确定</el-button>
		</span>
	</el-dialog>
</template>

<script>
	import {
		isEmail,
		isMobile
	} from '@/utils/validate'
	import {
		Debounce
	} from '@/utils/debounce'
	import {
		encrypt
	} from '@/utils/crypto'
	export default {
		data() {
			var validatePassword = (rule, value, callback) => {
				if (!this.dataForm.id && !/\S/.test(value)) {
					callback(new Error('密码不能为空'))
				} else {
					callback()
				}
			}
			var validateComfirmPassword = (rule, value, callback) => {
				if (!this.dataForm.id && !/\S/.test(value)) {
					this.dataForm.password = ''
					callback(new Error('确认密码不能为空'))
				} else if (this.dataForm.password !== value) {
					callback(new Error('确认密码与密码输入不一致'))
				} else {
					callback()
				}
			}
			var validateEmail = (rule, value, callback) => {
				if (!isEmail(value)) {
					callback(new Error('邮箱格式错误'))
				} else {
					callback()
				}
			}
			var validateMobile = (rule, value, callback) => {
				if (!isMobile(value)) {
					callback(new Error('手机号格式错误'))
				} else {
					callback()
				}
			}
			return {
				visible: false,
				roleList: [],
				dataForm: {
					id: 0,
					userName: '',
					password: '',
					comfirmPassword: '',
					email: '',
					mobile: '',
					roleIdList: [],
					status: 1
				},
				dataRule: {
					userName: [{
							required: true,
							message: '用户名不能为空',
							trigger: 'blur'
						},
						{
							pattern: /\s\S+|S+\s|\S/,
							message: '请输入正确的用户名',
							trigger: 'blur'
						}
					],
					password: [{
						validator: validatePassword,
						trigger: 'blur'
					}],
					comfirmPassword: [{
						validator: validateComfirmPassword,
						trigger: 'blur'
					}],
					email: [{
							required: true,
							message: '邮箱不能为空',
							trigger: 'blur'
						},
						{
							validator: validateEmail,
							trigger: 'blur'
						}
					],
					mobile: [{
							required: true,
							message: '手机号不能为空',
							trigger: 'blur'
						},
						{
							validator: validateMobile,
							trigger: 'blur'
						}
					]
				},
              registerForm: {
                userName : "",
                password : "",
                passwordAgian : "",
                userType : "area",
                area: '',
                city: '',
                province: '',
                areaId: null,
                cityId: null,
                provinceId: null,
                mobile: "",
                wxid: ""
              },
              registerRules: {
                userName: [{
                  required: true,
                  message: '帐号不能为空',
                  trigger: 'blur'
                }],
                password: [{
                  required: true,
                  message: '密码不能为空',
                  trigger: 'blur'
                }],
                passwordAgian: [{
                  required: true,
                  message: '请确认密码',
                  trigger: 'blur'
                }],
                mobile: [{
                  required: true,
                  message: '手机号不能为空',
                  trigger: 'blur'
                }],
              },
			}
		},
		methods: {
			init(id) {
				this.dataForm.id = id || 0
				this.$http({
					url: this.$http.adornUrl('/sys/role/list'),
					method: 'get',
					params: this.$http.adornParams()
				}).then(({
					data
				}) => {
					this.roleList = data
				}).then(() => {
					this.visible = true
					this.$nextTick(() => {
						this.$refs.dataForm.resetFields()
					})
				}).then(() => {
					if (this.dataForm.id) {
						this.$http({
							url: this.$http.adornUrl(`/sys/user/info/${this.dataForm.id}`),
							method: 'get',
							params: this.$http.adornParams()
						}).then(({
							data
						}) => {
							this.dataForm.userName = data.username
							this.dataForm.email = data.email
							this.dataForm.mobile = data.mobile
							this.dataForm.roleIdList = data.roleIdList
							this.dataForm.status = data.status
						})
					}
				})
			},
			// 表单提交
			dataFormSubmit: Debounce(function() {
				this.$refs['dataForm'].validate((valid) => {
					if (valid) {
						this.$http({
							url: this.$http.adornUrl(`/sys/user`),
							method: this.dataForm.id ? 'put' : 'post',
							data: this.$http.adornData({
								'userId': this.dataForm.id || undefined,
								'username': this.dataForm.userName,
								'password': encrypt(this.dataForm.password),
								'email': this.dataForm.email,
								'mobile': this.dataForm.mobile,
								'status': this.dataForm.status,
								'roleIdList': this.dataForm.roleIdList
							})
						}).then(({
							data
						}) => {
							this.$message({
								message: '操作成功',
								type: 'success',
								duration: 1500,
								onClose: () => {
									this.visible = false
									this.$emit('refreshDataList')
								}
							})
						})
					}
				})
			})
		}
	}
</script>
